package com.origin.multimedia.music

import android.content.Context
import android.view.View
import com.origin.multimedia.music.bean.MusicInfo
import com.origin.multimedia.music.service.MediaService

object MusicControlUtils {
    interface IPlayStatusLister {

        /**
         * 播放状态改变回调，根据情况是否复写
         */
        fun playStatusChange(@PlayStatusAnnotation.PLAY_STATUS playStatus: Int){

        }

        /**
         * 切换为播放状态
         */
        fun doPlay()

        /**
         * 切换为暂停或停止状态
         */
        fun doPause()

        /**
         * 刷新信息
         */
        fun doRefresh(musicInfo: MusicInfo)

        fun bufferingProgress(progress:Int)

        /**
         * @param progress 0-100
         * @param progressTime 00:00:00
         */
        fun playProgress(progress:Int,progressTime:String)
    }

    var playStatusLister: IPlayStatusLister? = null
    fun setIPlayStatusLister(iPlayStatusLister: IPlayStatusLister?) {
        playStatusLister = iPlayStatusLister
    }
    private var playStatus = 0;

    public fun playControl(context: Context) {
        if (playStatus == PlayStatusAnnotation.PAUSE) {
            MediaService.continuePlay(context)
        } else if (playStatus == PlayStatusAnnotation.STOP_PLAY_SERVICE) {
            MediaService.startPlay(context, 0)
        } else {
            MediaService.pausePlay(context)
        }
    }

    fun previousListener(context: Context) {
        MediaService.previousPlay(context)
    }

    fun nextListener(context: Context) {
        MediaService.nextPlay(context)
    }
    fun bufferingProgress(progress:Int){
        playStatusLister?.bufferingProgress(progress)
    }
    fun playProgress(progress:Int,progressTime:String){
        playStatusLister?.playProgress(progress,progressTime)
    }

    /**
     * 外部包不要调用
     */
    public fun setPlayStatus(@PlayStatusAnnotation.PLAY_STATUS _playStatus: Int) {
        playStatus = _playStatus;

        playStatusLister?.apply {
            val self = this
            playStatusChange(playStatus)
            playStatus.apply {
                val it = this
                if (it == PlayStatusAnnotation.PAUSE || it == PlayStatusAnnotation.STOP_PLAY_SERVICE) {
                    self.doPlay()
                } else if (it == PlayStatusAnnotation.PLAY || it == PlayStatusAnnotation.CONTINUE_PLAY) {
                    self.doPause()
                } else if (it == PlayStatusAnnotation.NEXT || it == PlayStatusAnnotation.PREVIOUS) {
                    self.doRefresh(MusicPlayList.getCurrentPlay())
                }
            }
        }
    }

}